package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.mapper.CourtRoomMapper;
import com.hxht.cmp.mapper.PlanMapper;
import com.hxht.cmp.mapper.TrialMapper;
import com.hxht.cmp.pojo.BasisCourt;
import com.hxht.cmp.service.StatisticalService;
import com.hxht.cmp.utils.CourtUidListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计相关
 */
@Service
public class StatisticalServiceImpl implements StatisticalService {

    @Autowired
    private CourtRoomMapper courtRoomMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private TrialMapper trialMapper;



    @Override
    public int getCountCourtByStatus(String courtUid, Integer status, String today) {
        List<String> courtUidList = CourtUidListUtils.getCourtUidList(courtUid);
        Map<String, Object> map = new HashMap<>();
        map.put("courtUidList", courtUidList);
        map.put("status", status);
        map.put("today", today);
        int num = planMapper.getCountCourtByStatus(map);
        return num;
    }

    @Override
    public int getCountOpenCourtNum(String courtUid) {
        List<String> courtUidList = CourtUidListUtils.getCourtUidList(courtUid);
        int trialNum = planMapper.getCountOpenCourtNum(courtUidList);
        return trialNum;
    }

    @Override
    public Map<String, Integer> getCountCourtMap(String courtUid) {
        List<String> courtUidList = CourtUidListUtils.getAllCourUidList(courtUid);
        Map<String,Integer> countMap = new HashMap<>();
        int allCourtNum = 0;
        int technicalCourtNum=0;
        int thirdCourtNum=0;
        for (String cId : courtUidList) {
            for (BasisCourt basisCourt : Constants.COURT_CONFIG_LIST) {
                if (cId.equals(basisCourt.getUid())){
                    allCourtNum +=basisCourt.getAllCourtNum();
                    technicalCourtNum+=basisCourt.getTechnicalCourtNum();
                    thirdCourtNum+=basisCourt.getThirdCourtNum();

                }
            }
        }
        countMap.put("allCourtNum",allCourtNum);
        countMap.put("technicalCourtNum",technicalCourtNum);
        countMap.put("thirdCourtNum",thirdCourtNum);
        return countMap;
    }

    @Override
    public int getCountFileSize(String courtUid) {
        List<String> courtUidList = CourtUidListUtils.getCourtUidList(courtUid);
        int fileSizeTotal =planMapper.getCountFileSize(courtUidList);
        return fileSizeTotal;
    }

    @Override
    public int getCountOpenCourtTime(String courtUid) {
        List<String> courtUidList = CourtUidListUtils.getCourtUidList(courtUid);
        int countTime = trialMapper.getCountOpenCourtTime(courtUidList);
        return countTime;
    }

    @Override
    public List<Map<String, Object>> selectListJudgeOpenTimeAndOpenNum(String courtUid, String startDate, String endDate) {
        Map<String, String> param = new HashMap<>();
        param.put("courtUid", courtUid);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        //获取开庭数
        List<Map<String, Object>> openNums =planMapper.selectListJudgeOpenNum(param);
        //获取开庭时长
        List<Map<String, Object>> openTimes = trialMapper.selectListJudgeOpenTime(param);
        //结果
        List<Map<String,Object>> result = new ArrayList<>();
        for (Map<String, Object> openNum : openNums) {
            for (Map<String, Object> openTime : openTimes) {
                if(openNum.get("userUid").equals(openTime.get("userUid"))){
                    Map<String,Object> resultMap = new  HashMap<>();
                    if(ObjectUtils.isEmpty(openNum.get("realName"))){
                        continue;
                    }
                    for (BasisCourt basisCourt : Constants.COURT_CONFIG_LIST) {
                        if (basisCourt.getUid().equals(openNum.get("courtUid"))){
                            resultMap.put("courtName",basisCourt.getAbbreviation());
                        }
                    }
                    resultMap.put("userUid",openNum.get("userUid"));
                    resultMap.put("realName",openNum.get("realName"));
                    resultMap.put("courtUid",openNum.get("courtUid"));
                    resultMap.put("openNum",openNum.get("num"));
                    resultMap.put("openTime",openTime.get("timeTotal"));
                    result.add(resultMap);
                }
            }
        }
        return result;
    }
}
